<%@page contentType="text/html" pageEncoding="windows-1252"%>
<%@page import="java.sql.Date"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="startup.model.trip.MissionRequest"%>
<%@page import="startup.model.trip.Status"%>
<%@page import="startup.model.trip.Vehicle"%>
<%@page import="startup.model.user.Student"%>
<%@page import="startup.utils.DateTool"%>
<%@page import="startup.utils.InsertStatement"%>
<%@page import="startup.utils.Query"%>
<%
	try
	{
		Student student = ( Student )session.getAttribute( "currentSessionUser" );
		student.completeAttributesFromWorkingDB();

		try
		{

			MissionRequest toSubmit = ( MissionRequest )session.getAttribute( "missionRequest" );
			toSubmit.setPlannedStartDate( new Date( DateTool.getDateInMillis( request.getParameter( "startDate" ) ) ) );
			toSubmit.setPlannedEndDate( new Date( DateTool.getDateInMillis( request.getParameter( "endDate" ) ) ) );
			toSubmit.setDestination( request.getParameter( "destination" ) );
			toSubmit.setCurrentAddress( request.getParameter( "currentAddress" ) );
			toSubmit.setTypeOfTransportation( Vehicle.valueOf( request.getParameter( "typeOfTransportation" ) ) );

			String plannedMeans = request.getParameter( "planned_means_of_transports" );
			if( !toSubmit.getTypeOfTransportation().equals( Vehicle.BUSINESS ) )
			{
				toSubmit.setPlannedMeansOfTransports( "Means: " + plannedMeans +
													  " ;Model: " + request.getParameter( "modelOfPrivateVehicle" ) +
													  " ;License: " + request.getParameter( "license_plate" ) );
			}
			else
			{
				if( plannedMeans.indexOf( "Means: " ) >= 0 )
				{
					toSubmit.setPlannedMeansOfTransports( "Means: " + plannedMeans );
				}
				else
				{
					toSubmit.setPlannedMeansOfTransports( plannedMeans );
				}
			}
			toSubmit.setArePersonalFundsUsed( Boolean.parseBoolean( request.getParameter( "personalFund" ) ) );
			toSubmit.setNumberOfNights( Integer.parseInt( request.getParameter( "nights" ) ) );
			toSubmit.setPlannedReasonForTravel( request.getParameter( "reason" ) );
			toSubmit.setOtherDeclarations( request.getParameter( "otherDeclarations" ) );
			toSubmit.setIsStopoverRequested( Boolean.parseBoolean( request.getParameter( "isStopover" ) ) );
			if( toSubmit.getIsStopoverRequested() )
			{
				toSubmit.setReasonForStopover( request.getParameter( "stopoverReason" ) );
			}
			if( Boolean.parseBoolean( request.getParameter( "externalFund" ) ) )
			{
				Query query = new Query( "F.name,S.funds_owner", "students S,funding_institutions F", "user_ID=" +
																									  toSubmit.getOwnerStudentId() +
																									  " and S.funding_institution=F.funding_institution_ID" );
				ResultSet resultSet = query.executeOnPhdDatabase();
				resultSet.next();
				toSubmit.setFirstAlternativeFundName( resultSet.getString( "name" ) );
				toSubmit.setFirstAlternativeFundManagerId( resultSet.getInt( "funds_owner" ) );
			}
			toSubmit.setSecondAlternativeFundName( request.getParameter( "secondFundName" ) );
			if( toSubmit.getTypeOfTransportation().equals( Vehicle.EXTRAORDINARY ) )
			{
				toSubmit.setReasonForExtraordinaryTransport( request.getParameter( "reasonExtraordinaryTransport" ) );
			}
			toSubmit.setStatus( Status.INWAIT );
			InsertStatement insertStatement = new InsertStatement( toSubmit );
			insertStatement.executeOnWorkingDatabase();

			student.getHistoryFromDatabase();

		}
		catch( RuntimeException runtimeException )
		{
			request.setAttribute( "exception", runtimeException );
			request.getRequestDispatcher( "errorPage.jsp" ).forward( request, response );
		}
		finally
		{
			session.removeAttribute( "trip" );
		}
%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="windows-1252">
        <title>Request Sent</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <!-- Le styles -->
        <link href="styles/bootstrap.css" rel="stylesheet">
        <style type="text/css">
            body {
                padding-top: 60px;
                padding-bottom: 40px;
            }
            .sidebar-nav {
                padding: 9px 0;
            }
        </style>
    </head>
    <body>
        <!--Barra Superiore-->
        <div class="navbar navbar-fixed-top">
            <div class="navbar-inner">
                <div class="container-fluid">
                    <a class="brand" href="#">Student : <%=student.getSurname()%> <%=student.getName()%> </a>
                    <div class="nav pull-right">
                        <ul class="nav">
                            <li>
                                <form action="Logout" method="post">
                                    <button class="btn btn-primary btn-small">Sign out</button>
                                </form>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <!--CONTENUTO GRANDE -->
        <div class="container-fluid">
            <div class="row-fluid">
                <div class="span3" style="position:fixed;">
                    <div class="well sidebar-nav">
                        <ul class="nav nav-list">
                            <li class="nav-header">Location</li>
                            <li><a href="studentChooseHistoryToShow.jsp"><i class="icon-home"></i> Home</a></li>
                            <li><a href="studentActualHistory.jsp"> &nbsp; &bull; <i class="icon-th-large"></i> ACTIVE History</a></li>
                            <li><a href="studentTerminatedHistory.jsp"> &nbsp; &bull; <i class="icon-th-large"></i> TERMINATED History</a></li>
                            <li><a href="studentDeletedHistory.jsp"> &nbsp; &bull; <i class="icon-th-large"></i> DELETED History</a></li>
                            <li class="nav-header">Function</li>
                            <li><a href="studentNewMissionRequestSubmission.jsp"><i class="icon-pencil"></i> New Mission Request</a></li>
                            <li><a href="#"> &nbsp; &bull; <i class="icon-tasks"></i><del> Complete the forms </del></a></li>
                            <li class="active"><a href="#"> &nbsp;&nbsp; &bull; <i class="icon-white icon-ok"></i> Update </a></li>
                            <li><a href="studentShowNewAdvanceCompensation.jsp"><i class="icon-pencil"></i> New Advance Compensation</a></li>                            
                            <li><a href="studentShowNewReimbursement.jsp"><i class="icon-pencil"></i> New Reimbursement</a></li>
                            <li class="divider"></li>
                            <li><a href="studentFAQ.jsp"><i class="icon-info-sign"></i> F.A.Q.</a></li>             
                        </ul>
                    </div><!--/.well -->
                </div><!--/span-->
                <div class="span9" style="margin-left: 26%;">
                    <div class="alert alert-success">
                        <h2 class="alert-heading">Update </h2><br />
                        <h3>Success!</h3><br />
                        <p>
                            Request succesfully send.
                        </p>
                    </div>
                </div>
            </div>
        </div><!--/.fluid-container-->
        <div class="navbarCC navbar-fixed-bottom">
            <div class="navbarCC-inner">
                <div class="container-fluid">				
                    <a class="brand" href="#">&copy; 2012 Startup</a>					
                </div>
            </div>
        </div>
		<!-- Il javascript -->
		<script src="js/jquery.js"></script>
		<script src="js/bootstrap.js"></script>
	</body>
</html>
<%
	}
	catch( SQLException sqlException )
	{
		sqlException = new SQLException( "The database is currently busy.<br />" +
										 "If the problem persists, please report it to the administration." );
		request.setAttribute( "exception", sqlException );
		request.getRequestDispatcher( "errorPage.jsp" ).forward( request, response );
	}
	catch( RuntimeException runtimeException )
	{
		if( runtimeException.getClass().equals( NullPointerException.class ) )
		{
			runtimeException = new NullPointerException( "The session has timed out:<br />" + "" +
														 "try reloading page.<br />" +
														 "If the problem persists, please report it to the administration." );
		}
		request.setAttribute( "exception", runtimeException );
		request.getRequestDispatcher( "errorPage.jsp" ).forward( request, response );
	}
%>